package com.item.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.item.demo.VO.ResultVo;
import com.item.demo.config.ApiConfig;
import com.item.demo.entity.Users;
import com.item.demo.utils.EspHttpClient;
import com.item.demo.utils.EspHttpClientBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import static com.item.demo.utils.HttpClientConfig.espHttpClientProvider;


@Api(tags = "签约后台Api管理")
@RestController
@RequestMapping("/test")
@Slf4j
public class ApiController {
    String baseUrl = ApiConfig.getBaseUrl();
    String apiId = ApiConfig.getApiId();
    String apiSecret = ApiConfig.getApiSecret();

    @ApiOperation(value = "创建新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personalName", dataType = "String", required = true, paramType = "query", value = "用户名字"),
            @ApiImplicitParam(name = "idCardNum", dataType = "String", required = true, paramType = "query", value = "身份证号"),
            @ApiImplicitParam(name = "mobile", dataType = "String", required = true, paramType = "query", value = "电话号码"),
            @ApiImplicitParam(name = "authCode", dataType = "String", required = true, paramType = "query", value = "终端用户填写的短信验证码"),
            @ApiImplicitParam(name = "clientIp", dataType = "String", paramType = "query", value = "可选字段，终端用户的网络 ip 地址"),
            @ApiImplicitParam(name = "identity", dataType = "String", required = true, paramType = "query", value = "认证参数")
    })
    @PostMapping("/user/create")
    public ResultVo createUser(
            @RequestParam(name = "authCode") String authCode,
            @RequestParam(name = "personalName") String personalName,
            @RequestParam(name = "idCardNum") String idCardNum,
            @RequestParam(name = "mobile") String mobile,
            @RequestParam(name = "clientIp", required = false) String clientIp,
            @RequestParam(name = "identity") String identity) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/user/create";
        Map<String, Object> user = new HashMap<String, Object>();
        user.put("userType", 0);
        user.put("personalName", personalName);
        user.put("idCardNum", idCardNum);
        user.put("mobile", mobile);
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        reqData.put("user", user);
        reqData.put("authCode", authCode);
        reqData.put("clientIp", clientIp);
        reqData.put("identity", identity);
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }

//    @ApiOperation(value = "更新用户信息", httpMethod = "POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "authCode", dataType = "String", required = true, paramType = "query", value = "终端用户填写的短信验证码"),
//            @ApiImplicitParam(name = "clientIp", dataType = "String", paramType = "query", value = "可选字段，终端用户的网络 ip 地址"),
//            @ApiImplicitParam(name = "oldAuthCode", dataType = "String", paramType = "query", value = "可选字段，旧手机的短信验证码"),
//            @ApiImplicitParam(name = "newAuthCode", dataType = "String", required = false, paramType = "query", value = "，新手机的短信验证码，手机号改变时必传，其它情况不传")
//    })
//    @PostMapping("/user/update")
//    public ResultVo update(
//            @RequestParam(name = "users") Users users,
//            @RequestParam(name = "authCode", required = false) String clientIp,
//            @RequestParam(name = "clientIp", required = false) String oldAuthCode,
//            @RequestParam(name = "newAuthCode", required = false) String newAuthCode) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
//        String url = baseUrl + "/user/update";
//        Map<String, Object> reqData = new HashMap<String, Object>();
//        reqData.put("apiId", apiId);
//        reqData.put("timestamp", System.currentTimeMillis());
//        ///
//        reqData.put("users", users);
//        reqData.put("clientIp", clientIp);
//        reqData.put("oldAuthCode", oldAuthCode);
//        reqData.put("newAuthCode", newAuthCode);
//        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
//        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
//        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
//        log.info(strJsonRespData);
//        return ResultVo.oK(jsonResp);
//    }

    @ApiOperation(value = "获取用户信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", dataType = "String", required = true, paramType = "query", value = "用户标识")
    })
    @PostMapping("/user/get")
    public ResultVo getUser(
            @RequestParam(name = "userId") String userId) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/user/get";
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        reqData.put("userId", userId);
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }

    @ApiOperation(value = "创建合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contract", dataType = "Object", required = true, paramType = "query", value = "object 对象，contractRaw"),
            @ApiImplicitParam(name = "docRequired", dataType = "boolean", required = true, paramType = "query", value = "可选字段，布尔型，值为 true 指示返回值需包含合同文档"),
            @ApiImplicitParam(name = "applyForWitness", dataType = "boolean", required = true, paramType = "query", value = "可选字段，布尔型，值为 true 指定为这份合同申请见证服务"),
            @ApiImplicitParam(name = "legalDocType", dataType = "String", required = true, paramType = "query", value = "String 类型，可选字段，当申请见证服务时需要。法律文书类型，固定为\"签署见证意见书\"")
    })
    @PostMapping("/contract/creat")
    public ResultVo createContract(
            @RequestParam(name = "contract") String contract,
            @RequestParam(name = "docRequired", required = false) boolean docRequired,
            @RequestParam(name = "applyForWitness", required = false) boolean applyForWitness,
            @RequestParam(name = "legalDocType", required = false) String legalDocType) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/contract/creat";
        //参数数组
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        reqData.put("contract", contract);
        reqData.put("docRequired", docRequired);
        reqData.put("applyForWitness", applyForWitness);
        reqData.put("legalDocType", legalDocType);
        //创建请求
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }

    @ApiOperation(value = "获取合同图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contract", dataType = "Object", required = true, paramType = "query", value = "合同标识"),
            @ApiImplicitParam(name = "pageNum", dataType = "Integer", required = true, paramType = "query", value = "可选参数，页数，页号从 1 开始，默认为 1"),
            @ApiImplicitParam(name = "pageSize", dataType = "Integer", required = true, paramType = "query", value = "可选参数，项数，默认 20，最大 50，超过 50 默认50")
    })
    @PostMapping("/contract/getImages")
    public ResultVo getImages(
            @RequestParam(name = "contract") String contract,
            @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "20") Integer pageSize) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/contract/getImages";
        //参数数组
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        ////参数设置
        reqData.put("contract", contract);
        reqData.put("pageNum", pageNum);
        reqData.put("pageSize", pageSize);
        //创建请求
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }

    @ApiOperation(value = "获取合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractId", dataType = "String", required = true, paramType = "query", value = "合同标识"),
            @ApiImplicitParam(name = "docRequired", dataType = "String", paramType = "query", value = "可选字段，布尔型，值为 true 指示返回值需包含合同文档")
    })
    @PostMapping("/contract/get")
    public ResultVo getContract(
            @RequestParam(name = "contractId") String contractId,
            @RequestParam(name = "docRequired", required = false) String docRequired) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/contract/get";
        //参数数组
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        reqData.put("contractId", contractId);
        reqData.put("docRequired", docRequired);
        //创建请求
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }

    @ApiOperation(value = "获取合同参与者")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractId", dataType = "String", required = true, paramType = "query", value = "合同标识")
    })
    @PostMapping("/contract/getUserList")
    public ResultVo getUserList(
            @RequestParam(name = "contractId") String contractId,
            @RequestParam(name = "docRequired", required = false) String docRequired) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/contract/getUserList";
        //参数数组
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        reqData.put("contractId", contractId);
        //创建请求
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }

    @ApiOperation(value = "签署合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractId", dataType = "String", required = true, paramType = "query", value = "合同标识"),
            @ApiImplicitParam(name = "userId", dataType = "String", required = true, paramType = "query", value = "参与者的用户标识，以 UUID 表示"),
            @ApiImplicitParam(name = "operatorId", dataType = "String", required = false, paramType = "query", value = "可选字段，操作员的用户标识，以 UUID 表示"),
            @ApiImplicitParam(name = "signatureId", dataType = "String", required = false, paramType = "query", value = "可选字段，签章标识，指示在合同内的哪个签章上签名，不传此参数表示在该参与者的所有签章上签名"),
            @ApiImplicitParam(name = "authCode", dataType = "String", required = true, paramType = "query", value = "终端用户填写的短信验证码"),
            @ApiImplicitParam(name = "authSign", dataType = "String", required = false, paramType = "query", value = "可选，authSign，用于授权证书签署"),
            @ApiImplicitParam(name = "clientIp", dataType = "String", required = false, paramType = "query", value = "可选字段，终端用户的网络 ip 地址"),
            @ApiImplicitParam(name = "docRequired", dataType = "String", required = true, paramType = "query", value = "值为 true 指示返回值需包含合同文档")
    })
    @PostMapping("/contract/sign")
    public ResultVo sign(
            @RequestParam(name = "contractId") String contractId,
            @RequestParam(name = "userId", required = true) String userId,
            @RequestParam(name = "operatorId", required = true) String operatorId,
            @RequestParam(name = "signatureId", required = true) String signatureId,
            @RequestParam(name = "authCode", required = true) String authCode,
            @RequestParam(name = "authSign", required = true) String authSign,
            @RequestParam(name = "clientIp", required = true) String clientIp,
            @RequestParam(name = "docRequired", required = true) String docRequired
    ) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/contract/sign";
        //参数数组
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        ////参数设置
        reqData.put("contractId", contractId);
        reqData.put("userId", userId);
        reqData.put("operatorId", operatorId);
        reqData.put("signatureId", signatureId);
        reqData.put("authCode", authCode);
        reqData.put("authSign", authSign);
        reqData.put("clientIp", clientIp);
        reqData.put("docRequired", docRequired);
        //创建请求
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }
    @ApiOperation(value = "创建用户印章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", dataType = "String", required = true, paramType = "query", value = "参与者的用户标识，以 UUID 表示"),
            @ApiImplicitParam(name = "seal", dataType = "String", required = false, paramType = "query", value = "可选字段，操作员的用户标识，以 UUID 表示")
    })
    @PostMapping("/seal/createByUser")
    public ResultVo createByUser(
            @RequestParam(name = "contractId") String contractId,
            @RequestParam(name = "userId", required = true) String userId,
            @RequestParam(name = "operatorId", required = true) String operatorId,
            @RequestParam(name = "signatureId", required = true) String signatureId,
            @RequestParam(name = "authCode", required = true) String authCode,
            @RequestParam(name = "authSign", required = true) String authSign,
            @RequestParam(name = "clientIp", required = true) String clientIp,
            @RequestParam(name = "docRequired", required = true) String docRequired
    ) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        String url = baseUrl + "/seal/createByUser";
        //参数数组
        Map<String, Object> reqData = new HashMap<String, Object>();
        reqData.put("apiId", apiId);
        reqData.put("timestamp", System.currentTimeMillis());
        ////参数设置
        reqData.put("contractId", contractId);
        reqData.put("userId", userId);
        reqData.put("operatorId", operatorId);
        reqData.put("signatureId", signatureId);
        reqData.put("authCode", authCode);
        reqData.put("authSign", authSign);
        reqData.put("clientIp", clientIp);
        reqData.put("docRequired", docRequired);
        //创建请求
        EspHttpClient espHttpClient = EspHttpClientBuilder.getHttpClient(espHttpClientProvider);
        String strJsonRespData = espHttpClient.postSignedJson(url, new JSONObject(reqData), apiSecret);
        JSONObject jsonResp = JSONObject.parseObject(strJsonRespData);
        log.info(strJsonRespData);
        return ResultVo.oK(jsonResp);
    }
}
